package com.boot.security.server.utils;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**************************************
 * @program: hotdog
 * @author: Bruce.Cheng
 * @desc: 导出工具类
 * @date: 2018-12-22 20:30
 **************************************/
public class ExportUtil {

    /**
     * @param response 请求
     * @param workbook excel 数据
     * @param filename 文件名
     * @return void
     * @author Bruce.Cheng
     * @desc 数据写入 response
     * @date 2018-12-22 20:29
     */
    public static void dataWriteResponse(HttpServletResponse response, XSSFWorkbook workbook, String filename) throws IOException {
        response.reset(); //清除buffer缓存
        // response.setContentType("application/vnd.ms-excel;charset=UTF-8");
       // response.setHeader("Content-Disposition", "attachment;filename=" + filename);
       // response.setHeader("Pragma", "no-cache");
       // response.setHeader("Cache-Control", "no-cache");
        response.setContentType("application/x-download");//下面三行是关键代码，处理乱码问题
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("gbk"), "iso8859-1")+".xls");
       // response.setDateHeader("Expires", 0);
        // 写入数据
        workbook.write(response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}
